package com.weichat.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.dao.LocationMapper;
import com.model.Event_user;
import com.model.Location;
import com.model.Propertr_user;
import com.model.User;
import com.service.Event_userService;
import com.service.LocationService;
import com.service.Propertr_userService;
import com.service.UserService;
import com.service.Wx_access_tokenService;
import com.weichat.tool.AccessTokentool;
import com.weichat.tool.Sha1Util;
import com.weichat.tool.WeiChatConfig;
import com.weichat.tool.WeiChatUtil;

@RestController
public class JEZController {
	@Autowired
	LocationService locationService;
	
	@Autowired
	Event_userService event_userService;
	
	@Autowired
	Propertr_userService propertr_userService;
	/**
	 * 
	 * 将营车位置传到数据库中
	 * 
	 * @param location
	 * @return
	 */
	//
	@RequestMapping(value = "/shareCar", method = RequestMethod.POST)
	public boolean insertLocation(HttpServletRequest request,@RequestParam("latitude") BigDecimal latitude,@RequestParam("longitude") BigDecimal longitude ) {
		System.out.println(11);	
		boolean flag=false;
		User u = (User) request.getSession().getAttribute("user");		
		System.out.println("latitude-----"+latitude);
		System.out.println("longitude-------"+longitude);
		Location l=new Location();
		l.setLatitude(latitude);
		l.setLongitude(longitude);
		l.setuId(u.getId());
		System.out.println("u.getId()----------"+u.getId());
		//l.setuId(16);		
		if(locationService.updateSchoolBus(l)>0){
			flag=true;
		}
		return flag;
	}
	/**
	 * 
	 * 将我的位置传到数据库中
	 * 
	 * @param location
	 * @return
	 */
	@RequestMapping(value = "/shareMyself", method = RequestMethod.POST)
	@ResponseBody
	
	public Location insertMyselfLocation(HttpServletRequest request,@RequestParam("latitude") BigDecimal latitude,@RequestParam("longitude") BigDecimal longitude ) {
		System.out.println(66);	
		Location l = new Location();
		User u = (User) request.getSession().getAttribute("user");		
		System.out.println("latitude-----"+latitude);
		System.out.println("longitude-------"+longitude);
		l.setLatitude(latitude);
		l.setLongitude(longitude);
		l.setuId(u.getId());
		//l.setuId(121);
		//更改自己的位置，再把位置给查出来
		
		if(locationService.updateSchoolBus(l)>0){
			l = locationService.selectNewBus(u.getId());
		}
		return l;
	}
	
	/**
	 * 
	 * 查看营车位置
	 * 
	 * @param location
	 * @return
	 */
	@RequestMapping(value = "/checkCar", method = RequestMethod.POST)
	public Location selectCarByCarId(HttpServletRequest request) {
		System.out.println(21);
		User u = (User) request.getSession().getAttribute("user");
		//String openid = u.getOpenid();
		Location l = locationService.selectCarByCarId("opkTIv_cBPU-z9AkdyOpWwV6s-jA");
		//Location l = locationService.selectCarByCarId(u.getOpenid());
		//System.out.println("u.getOpenid()-----"+u.getOpenid());
		return l;
	}
	
	/**
	 * 
	 * 查看天工自己位置
	 * 
	 * @param location
	 * @return
	 */
	@RequestMapping(value = "/getTg", method = RequestMethod.POST)
	public Location selectCarByCarId2(HttpServletRequest request) {
		System.out.println(21);
		User u = (User) request.getSession().getAttribute("user");
		//Location l = locationService.selectCarByCarId(u.getOpenid());
		Location l = locationService.selectCarByCarId("opkTIv_cBPU-z9AkdyOpWwV6s-jA");
		//System.out.println("u.getOpenid()-----"+u.getOpenid());
		return l;
	}
	
	
	/**
	 * 将天工位置传到数据库中
	 */
	@RequestMapping(value = "/checkPosition", method = RequestMethod.POST)
	public boolean insertLocation2(HttpServletRequest request, @RequestParam("latitude") BigDecimal latitude,
			@RequestParam("longitude") BigDecimal longitude) {
		System.out.println("latitude-------"+latitude);
		System.out.println("longitude------"+longitude);
		boolean flag=false;
		User u = (User) request.getSession().getAttribute("user");
		Location l = new  Location();
		l.setLatitude(latitude);
		l.setLongitude(longitude);
		l.setuId(u.getId());
		//l.setuId(16);
		//System.out.println("u.getId()--------"+u.getId());
		List<Location> ls=locationService.selectUser(u.getId());
		//List<Location> ls=locationService.selectUser(16);
		if(ls.size()==0){
			locationService.insertSelective(l);
			System.out.println(7777);
			flag=true;
		}else if(locationService.updateAllLocation(l)>0){
			flag=true;
			System.out.println(8888);
		}
		return flag;
	}
	
	
	/**
	 * 
	 * 查看所有人位置
	 * 
	 * @param location
	 * @return
	 */
	@RequestMapping(value = "/checkpeople", method = RequestMethod.POST)
	public List<Location> selectAllPeopleLocation(HttpServletRequest request){
		System.out.println(33);
		User u = (User) request.getSession().getAttribute("user");
		//int id=u.getuId();			
		List<Location> ls=locationService.selectAllPeopleLocation();		
		System.out.println(ls.get(2).getName());
		return ls;
	}
	 /**
	  * 
	  * 点击乘车,＋100%执行能力//  ＋100%信誉度
	  */
	@RequestMapping(value = "/TakeBus", method = RequestMethod.POST)
	public String InsertTakeBus(HttpServletRequest request) throws Exception{
		String qw = null;
		System.out.println(44);
		User u = (User) request.getSession().getAttribute("user");	
		
		int id;
		Event_user eu=new Event_user();		
		eu.setEventId(3);
		eu.setType("Y");
		eu.setUserId(u.getId());
		
		Propertr_user pu1=new Propertr_user();
		pu1.setCount(100);
		pu1.setpId(2);
		pu1.setuId(u.getId());
		pu1.setEventName("乘车");
		
		Propertr_user pu2=new Propertr_user();
		pu2.setCount(100);
		pu2.setpId(3);
		pu2.setuId(u.getId());
		pu2.setEventName("乘车");
		
		if(0<event_userService.insertSelective(eu)){
			id=event_userService.selectEidByUidOrder(u.getId()).getEuId();		
			pu2.setEuId(id);
			pu1.setEuId(id);
		}
		
		
		if((0<propertr_userService.insertSelective(pu1)&&(0<propertr_userService.insertSelective(pu2)))){
			qw="Y";
		}
		
		
		return qw;
	}
	
	 /**
	  * 
	  * 点击睡觉,奖励5元勤学金、午休睡觉执行能力100分、午休睡觉信誉度100分
	  */
	@RequestMapping(value = "/siesta", method = RequestMethod.POST)
	public String InsertSiesta(HttpServletRequest request) throws Exception{
		String as= null;
		System.out.println(44);
		int id;
		User u = (User) request.getSession().getAttribute("user");	
		
		Event_user eu=new Event_user();		
		eu.setEventId(5);
		eu.setType("Y");
		eu.setUserId(u.getId());
		
		Propertr_user pu1=new Propertr_user();
		pu1.setCount(100);
		pu1.setpId(2);
		pu1.setuId(u.getId());
		pu1.setEventName("午睡");
		
		Propertr_user pu2=new Propertr_user();
		pu2.setCount(100);
		pu2.setpId(3);
		pu2.setuId(u.getId());
		pu2.setEventName("午睡");
		
		Propertr_user pu3=new Propertr_user();
		pu3.setCount(5);
		pu3.setpId(1);
		pu3.setuId(u.getId());
		pu3.setEventName("午睡");
		
		if(0<event_userService.insertSelective(eu)){
			id=event_userService.selectEidByUidOrder(u.getId()).getEuId();		
			pu1.setEuId(id);
			pu2.setEuId(id);
			pu3.setEuId(id);
		}
		
		
		if((0<propertr_userService.insertSelective(pu1)&&(0<propertr_userService.insertSelective(pu2))&&(0<propertr_userService.insertSelective(pu3)))){
			as="Y";
		}
		
		return as;
	}
	
}
